/*
 * Lightbox (for jQuery) based on jquery ui
 * version: 0.8 beta (29/10/2010)
 * 
 * @requires jQuery v1.4 or later
 * @requires jquery.ui.dialog plugin
 * @author nico.rehwaldt
 */
(function($) {

    // add and remove button support for jquery ui dialog
    // See http://old.nabble.com/jQuery-dialog-add-remove-button-on-the-fly-td22036498s27240.html
    $.extend($.ui.dialog.prototype, {
        'addbutton': function(name, func) {
            var buttons = this.element.dialog('option', 'buttons');
            buttons[name] = func;
            this.element.dialog('option', 'buttons', buttons);
        }
    });

    $.extend($.ui.dialog.prototype, {
        'removebutton': function(name) {
            var buttons = this.element.dialog('option', 'buttons');
            delete buttons[name];
            this.element.dialog('option', 'buttons', buttons);
        }
    });

    $.extend($.fn, {
        lightbox: function(options) {

            var dialog = $(this);
            if (dialog.attr("id") && $(dialog).parents("body").length == 0) {
                var d = $("#" + dialog.attr("id"));
                if (d.length > 0) {
                    dialog = d;
                    dialog.empty().append(d.children().clone());
                }
            }

            dialog = $.fn.dialog.apply(dialog, arguments);

            var dialogLoadComplete = function() {
                $(dialog).find("a.ajax").click(function(event) {
                    event.preventDefault();
                    dialog
                        .trigger("lightbox.beforeLoad")
                        .load($(this).attr("href"), dialogLoadComplete);
                });

                // Add submit = OK button to lightbox
                // if submitable form is found
                var form = $(dialog).find("form.ajax").ajaxForm({
                    target: dialog,
                    success: dialogLoadComplete
                });

                var submit = form.find("input[type=submit]");

                if (form.length > 0 && submit.length > 0) {
                    $(dialog).lightbox("addbutton", "Ok", function() {
                        $(this)
                            .trigger("lightbox.beforeLoad")
                            .find("form").submit();
                    });

                    submit.hide().parents("fieldset").hide();
                }

                // set title if content contains a h1 element
                var titleElement = $(dialog).find("h1");
                if (titleElement.length > 0) {
                    $(dialog).dialog({
                        title: titleElement.text()
                    });
                    titleElement.hide();
                }

                // Focus first focusable element in dialog
                $(dialog)
                    .find("input, select, textarea, button")
                    .eq(0).focus();
            };

            if (!dialog.is(".lightbox-bound")) {
                
                dialog
                    .ajaxSend(function(event, request, options) {
                        dialog.parent()
                            .find(".ui-dialog-titlebar .ui-dialog-title")
                            .append("<span class='loading blue-bg'></span>");
                    })
                    .ajaxComplete(function(event, request, options) {
                        dialog.parent()
                            .find(".ui-dialog-titlebar .ui-dialog-title .loading")
                            .remove();
                    });

                dialog.bind("lightbox.beforeLoad", function() {
                    $(this).dialog("removebutton", "Ok");
                });

                dialog.addClass("lightbox-bound");
            }

            if (typeof options == "object" && options.content) {
                dialog.trigger("lightbox.beforeLoad");
                dialog.load(options.content, dialogLoadComplete);
            }
            
            return dialog;
        }
    });
})(jQuery);